package com.dxf.chepter08

/**
 * 迭代器:
 *  iterator
 *  hasNext()
 *  next()
 *
 *
 * iterator 构建实际是 AbstractIterator 的一个匿名子类
 *  def iterator: Iterator[A] = if (self.isEmpty) Iterator.empty else new AbstractIterator[A]{
 *      xxx
 *  }
 *
 *
 * AbstractIterator[T]子类继承的特质 Iterator[T] 提供了netx hasNext 方法
 *
 *   def hasNext: Boolean = !these.isEmpty
 *   def next(): A =
 *       if (hasNext) {
 *         val result = these.head; these = these.tail; result
 *       } else Iterator.empty.next()
 *
 *
 *
 *
 *
 *
 */
object T101_迭代器 {


  def main(args: Array[String]): Unit = {
    val iterator= List(1,2,3,4,5).iterator

    println("=============1============")
    while (iterator.hasNext){
      println(iterator.next())
    }
    println("=============2============")

   var iterator2=List(1,2,3,4,5).iterator
    for (elem <- iterator2) {
      println(elem)//思考为什么为空
    }

  }







}

